package com.weiyin.examination.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.weiyin.examination.R;
import com.weiyin.examination.application.Constant;
import com.weiyin.examination.course.Course;
import com.weiyin.examination.course.CourseA1;
import com.weiyin.examination.course.CourseA10;
import com.weiyin.examination.course.CourseA2;
import com.weiyin.examination.course.CourseA3;
import com.weiyin.examination.course.CourseA4;
import com.weiyin.examination.course.CourseA5;
import com.weiyin.examination.course.CourseA6;
import com.weiyin.examination.course.CourseA7;
import com.weiyin.examination.course.CourseA8;
import com.weiyin.examination.course.CourseA9;
import com.weiyin.examination.course.CourseAudio1;
import com.weiyin.examination.course.CourseAudio2;
import com.weiyin.examination.dialog.SelectedTopicDialog;
import com.weiyin.examination.dialog.SubmitDialog;
import com.weiyin.examination.listener.CourseInterface;
import com.weiyin.examination.listener.DialogResultInterface;
import com.weiyin.examination.listener.DialogShowListener;
import com.weiyin.examination.listener.SelectTopicDialogResultListener;
import com.weiyin.examination.module.TestModule;
import com.weiyin.examination.util.LayoutHelper;
import com.weiyin.examination.util.LogUtil;
import com.weiyin.examination.util.SPUtil;
import com.weiyin.examination.util.ScreenUtil;
import com.weiyin.examination.util.VerifyCodeUtils;
import com.weiyin.examination.view.SurfaceViewTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * TopicActivity
 *
 * @author njf
 */
public class TopicActivity extends BaseActivity implements View.OnClickListener, CourseInterface,
        SelectTopicDialogResultListener, DialogShowListener, DialogResultInterface {
    private final String TAG = TopicActivity.class.getSimpleName();

    private SurfaceViewTemplate mTemplateView;
    private ImageView mStartBtn;
    private ViewFlipper practiceViewFlipper;
    private List<TestModule> testModuleList;
    private TestModule testModule;
    private List<Course> courseList;
    private TextView titleTextView, mainTitleTime;
    private Button submitButton;


    private String title;
    private int currentIndex;

    private SelectedTopicDialog selectedTopicDialog;
    private LayoutHelper layoutHelper;
    public static TopicActivity i;
    Handler mHandler = new Handler();
    Map<Integer, Boolean> map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_topic);
        i = this;
        map = new HashMap<>();
        layoutHelper = new LayoutHelper(this);
        layoutHelper.scaleView(findViewById(R.id.mainLayout));
        LogUtil.i(TAG, "width=" + ScreenUtil.getScreenWidth(this.getApplication()));
        LogUtil.i(TAG, "height=" + ScreenUtil.getScreenHeight(this.getApplication()));
        if (courseList == null) {
            courseList = new ArrayList<>();
        }
        testModuleList = getIntent().getParcelableArrayListExtra("list");
        initView();
        initViewData();
//
    }

    @Override
    protected void onDestroy() {
        for (int i = 0; i < courseList.size(); i++) {
            courseList.get(i).stop();
        }
        for (int i = 0; i < courseList.size(); i++) {
            courseList.get(i).destroy();
        }
        super.onDestroy();
        if (i != null) {
            i = null;
        }
    }

    public void initView() {
        submitButton = (Button) findViewById(R.id.submitButton);

        mainTitleTime = (TextView) findViewById(R.id.mainTitleTime);
        practiceViewFlipper = (ViewFlipper) findViewById(R.id.practiceViewFlipper);


        VerifyCodeUtils.sendCode(this, mainTitleTime, 7200);
        mainTitleTime.setOnClickListener(this);
        submitButton.setOnClickListener(this);
    }

    private void initViewData() {
        if (testModuleList != null) {
            for (int i = 0; i < testModuleList.size(); i++) {
                TestModule testModule = testModuleList.get(i);
                switch (testModule.getType()) {//张洋洋
                    case "1":
                        courseList.add(new CourseA1());
                        break;
                    case "2":
                        courseList.add(new CourseA2());
                        break;
                    case "3":
                        courseList.add(new CourseA3());
                        break;
                    case "4":
                        courseList.add(new CourseA4());
                        break;
                    case "5":
                        courseList.add(new CourseA5());
                        break;
                    case "6":
                        courseList.add(new CourseA6());
                        break;
                    case "7":
                        courseList.add(new CourseA7());
                        break;
                    case "8":
                        courseList.add(new CourseA8());
                        break;
                    case "9":
                        courseList.add(new CourseA9());
                        break;
                    case "17":
                        courseList.add(new CourseA10());
                        break;
                    case "19":
                        courseList.add(new CourseAudio1());
                        break;
                    case "18":
                        courseList.add(new CourseAudio2());
                        break;
                    default:
                        Constant.showTextToast(this, "不支持的类型，请更新版本");
                        finish();
                        break;
                }
            }
            SPUtil.setSharedPreferencesData(this, "titleNumber", courseList.size() + "");
            for (int i = 0; i < courseList.size(); i++) {
                Course step = courseList.get(i);
                step.onCreate(this, practiceViewFlipper, testModuleList, i);
                step.setCourseInterface(this);
                step.setCourseDialogListener(this);
            }
            currentIndex = 0;
            showNext(Constant.NEXT_KNOWLEDGE);
        }
        setTitle();
        initSelectTopicDialog();
    }

    private void initSelectTopicDialog() {
        if (testModuleList != null) {
            selectedTopicDialog = new SelectedTopicDialog(this, R.style.base_loading_dialogfragment, testModuleList.size());

            Window dialogWindow = selectedTopicDialog.getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);

            // 新位置Y坐标
            lp.y = layoutHelper.scaleDimension(50, "y");

            selectedTopicDialog.setSelectTopicDialogResultListener(this);
        }
    }

    private void setTitle() {
        titleTextView = (TextView) findViewById(R.id.mainTitleContent);
//        titleTextView.setText(title);
    }

    private void showNext(String type) {
//        setAnimation();
        if (currentIndex == 0 || practiceViewFlipper.getDisplayedChild() != currentIndex) {
            practiceViewFlipper.setDisplayedChild(currentIndex);
        }
        if (courseList.size() > 0) {
            courseList.get(currentIndex).show(testModuleList, currentIndex, type);
        }
    }

    private void showBack(String type) {
//        setBackAnimation();
        if (practiceViewFlipper.getDisplayedChild() != currentIndex) {
            practiceViewFlipper.setDisplayedChild(currentIndex);
        }
        courseList.get(currentIndex).show(testModuleList, currentIndex, type);
    }

    private void setAnimation() {
        practiceViewFlipper.setInAnimation(this, R.anim.general_right_in);
        practiceViewFlipper.setOutAnimation(this, R.anim.general_left_out);
    }

    private void setBackAnimation() {
        practiceViewFlipper.setInAnimation(this, R.anim.general_left_in);
        practiceViewFlipper.setOutAnimation(this, R.anim.general_right_out);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mainTitleTime:
//                startActivity(new Intent(TopicActivity.this, Main2Activity.class));
                break;
            case R.id.submitButton:
                SubmitDialog submitDialog = new SubmitDialog(this, R.style.base_loading_dialogfragment, number());
                submitDialog.show();
                submitDialog.setDialogResultInterface(this);
                break;
            default:
                break;
        }
    }

    private int number() {
        int number = 0;
        if (map != null) {
            for (int i = 0; i < testModuleList.size(); i++) {
                if (map.get(i) != null && map.get(i)) {
                    number++;
                }
            }
        }
        return testModuleList.size() - number;
    }

    @Override
    public void courseInterfaceResult(String type, int index) {
        switch (type) {
            case Constant.NEXT_KNOWLEDGE:
                LogUtil.i(TAG, "index=" + index);
                currentIndex = index;
                showNext(Constant.NEXT_KNOWLEDGE);
                break;
            case Constant.LAST_KNOWLEDGE:
                LogUtil.i(TAG, "index=" + index);
                currentIndex = index;
                showBack(Constant.LAST_KNOWLEDGE);
                break;
            default:

                break;
        }
    }


    @Override
    public void selectDialogResult(int position) {
        currentIndex = position;
        showNext(Constant.NEXT_KNOWLEDGE);
    }

    private void setResult(int position, boolean result) {
        if (selectedTopicDialog != null) {
            selectedTopicDialog.setTopicNumberList(position, result);
        }
    }

    @Override
    public void dialogShow(int result, int position, boolean answerResult) {
        if (result == 0) {
            if (selectedTopicDialog != null) {
                if (hasWindowFocus()) {
                    selectedTopicDialog.show();
                }
            }
        } else if (result == 1) {
            map.put(position, answerResult);
            setResult(position, answerResult);
        }
    }

    @Override
    public void result(String type, String result, String answerId) {
        if (type.equals(Constant.YES)) {
            startActivity(new Intent(TopicActivity.this, SubmitActivity.class));
            finish();
        }
    }
}